/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    can.c
  * @brief   This file provides code for the configuration
  *          of the CAN instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "can.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

CAN_HandleTypeDef hcan1;
CAN_HandleTypeDef hcan2;

/* CAN1 init function */
void MX_CAN1_Init(void)
{

  /* USER CODE BEGIN CAN1_Init 0 */

  /* USER CODE END CAN1_Init 0 */

  /* USER CODE BEGIN CAN1_Init 1 */

  /* USER CODE END CAN1_Init 1 */
  hcan1.Instance = CAN1;
  hcan1.Init.Prescaler = 3;
  hcan1.Init.Mode = CAN_MODE_NORMAL;
  hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan1.Init.TimeSeg1 = CAN_BS1_10TQ;
  hcan1.Init.TimeSeg2 = CAN_BS2_3TQ;
  hcan1.Init.TimeTriggeredMode = DISABLE;
  hcan1.Init.AutoBusOff = DISABLE;
  hcan1.Init.AutoWakeUp = DISABLE;
  hcan1.Init.AutoRetransmission = DISABLE;
  hcan1.Init.ReceiveFifoLocked = DISABLE;
  hcan1.Init.TransmitFifoPriority = DISABLE;
  if (HAL_CAN_Init(&hcan1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN CAN1_Init 2 */

  /* USER CODE END CAN1_Init 2 */

}
/* CAN2 init function */
void MX_CAN2_Init(void)
{

  /* USER CODE BEGIN CAN2_Init 0 */

  /* USER CODE END CAN2_Init 0 */

  /* USER CODE BEGIN CAN2_Init 1 */

  /* USER CODE END CAN2_Init 1 */
  hcan2.Instance = CAN2;
  hcan2.Init.Prescaler = 3;
  hcan2.Init.Mode = CAN_MODE_NORMAL;
  hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan2.Init.TimeSeg1 = CAN_BS1_10TQ;
  hcan2.Init.TimeSeg2 = CAN_BS2_3TQ;
  hcan2.Init.TimeTriggeredMode = DISABLE;
  hcan2.Init.AutoBusOff = DISABLE;
  hcan2.Init.AutoWakeUp = DISABLE;
  hcan2.Init.AutoRetransmission = DISABLE;
  hcan2.Init.ReceiveFifoLocked = DISABLE;
  hcan2.Init.TransmitFifoPriority = DISABLE;
  if (HAL_CAN_Init(&hcan2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN CAN2_Init 2 */

  /* USER CODE END CAN2_Init 2 */

}

static uint32_t HAL_RCC_CAN1_CLK_ENABLED=0;

void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(canHandle->Instance==CAN1)
  {
  /* USER CODE BEGIN CAN1_MspInit 0 */

  /* USER CODE END CAN1_MspInit 0 */
    /* CAN1 clock enable */
    HAL_RCC_CAN1_CLK_ENABLED++;
    if(HAL_RCC_CAN1_CLK_ENABLED==1){
      __HAL_RCC_CAN1_CLK_ENABLE();
    }

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**CAN1 GPIO Configuration
    PA11     ------> CAN1_RX
    PA12     ------> CAN1_TX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* CAN1 interrupt Init */
    HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
    HAL_NVIC_SetPriority(CAN1_SCE_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(CAN1_SCE_IRQn);
  /* USER CODE BEGIN CAN1_MspInit 1 */

  /* USER CODE END CAN1_MspInit 1 */
  }
  else if(canHandle->Instance==CAN2)
  {
  /* USER CODE BEGIN CAN2_MspInit 0 */

  /* USER CODE END CAN2_MspInit 0 */
    /* CAN2 clock enable */
    __HAL_RCC_CAN2_CLK_ENABLE();
    HAL_RCC_CAN1_CLK_ENABLED++;
    if(HAL_RCC_CAN1_CLK_ENABLED==1){
      __HAL_RCC_CAN1_CLK_ENABLE();
    }

    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**CAN2 GPIO Configuration
    PB12     ------> CAN2_RX
    PB13     ------> CAN2_TX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF9_CAN2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* CAN2 interrupt Init */
    HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
    HAL_NVIC_SetPriority(CAN2_SCE_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(CAN2_SCE_IRQn);
  /* USER CODE BEGIN CAN2_MspInit 1 */

  /* USER CODE END CAN2_MspInit 1 */
  }
}

void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
{

  if(canHandle->Instance==CAN1)
  {
  /* USER CODE BEGIN CAN1_MspDeInit 0 */

  /* USER CODE END CAN1_MspDeInit 0 */
    /* Peripheral clock disable */
    HAL_RCC_CAN1_CLK_ENABLED--;
    if(HAL_RCC_CAN1_CLK_ENABLED==0){
      __HAL_RCC_CAN1_CLK_DISABLE();
    }

    /**CAN1 GPIO Configuration
    PA11     ------> CAN1_RX
    PA12     ------> CAN1_TX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);

    /* CAN1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(CAN1_RX0_IRQn);
    HAL_NVIC_DisableIRQ(CAN1_SCE_IRQn);
  /* USER CODE BEGIN CAN1_MspDeInit 1 */

  /* USER CODE END CAN1_MspDeInit 1 */
  }
  else if(canHandle->Instance==CAN2)
  {
  /* USER CODE BEGIN CAN2_MspDeInit 0 */

  /* USER CODE END CAN2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_CAN2_CLK_DISABLE();
    HAL_RCC_CAN1_CLK_ENABLED--;
    if(HAL_RCC_CAN1_CLK_ENABLED==0){
      __HAL_RCC_CAN1_CLK_DISABLE();
    }

    /**CAN2 GPIO Configuration
    PB12     ------> CAN2_RX
    PB13     ------> CAN2_TX
    */
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12|GPIO_PIN_13);

    /* CAN2 interrupt Deinit */
    HAL_NVIC_DisableIRQ(CAN2_RX0_IRQn);
    HAL_NVIC_DisableIRQ(CAN2_SCE_IRQn);
  /* USER CODE BEGIN CAN2_MspDeInit 1 */

  /* USER CODE END CAN2_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */

void can_filter_init(void)
{

    CAN_FilterTypeDef can_filter_st;
    can_filter_st.FilterActivation = ENABLE;
    can_filter_st.FilterMode = CAN_FILTERMODE_IDMASK;
    can_filter_st.FilterScale = CAN_FILTERSCALE_32BIT;
    can_filter_st.FilterIdHigh = 0x0000;
    can_filter_st.FilterIdLow = 0x0000;
    can_filter_st.FilterMaskIdHigh = 0x0000;
    can_filter_st.FilterMaskIdLow = 0x0000;
    can_filter_st.FilterBank = 0;
    can_filter_st.FilterFIFOAssignment = CAN_RX_FIFO0;
	
    HAL_CAN_ConfigFilter(&hcan1, &can_filter_st);
    HAL_CAN_Start(&hcan1);
    HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
		HAL_CAN_ActivateNotification(&hcan1, CAN_IT_ERROR);                // ���ô����жϣ���ѡ��
		
    can_filter_st.SlaveStartFilterBank = 14;
    can_filter_st.FilterBank = 14;
	
    HAL_CAN_ConfigFilter(&hcan2, &can_filter_st);
    HAL_CAN_Start(&hcan2);
    HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
		HAL_CAN_ActivateNotification(&hcan2, CAN_IT_ERROR);                // ���ô����жϣ���ѡ��
}

//�����жϱ�־λ
uint16_t can_rx_flag=0;

//����can��������
uint8_t can_rx_data[8];

//储存电机反馈值
Motor_Feedback_t motor[3];  //存储电机的反馈值
Motor_Feedback_t motor_last[3];  //存储电机的反馈值

HAL_StatusTypeDef	HAL_RetVal;

uint16_t motor_flag[3]={2,2,2};

void  HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
	CAN_RxHeaderTypeDef rx_message;
	can_rx_flag++;
	
	if(hcan ==&hcan1)
    {
			HAL_RetVal = HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &rx_message,can_rx_data);
			
			if(HAL_RetVal == HAL_OK){
				
				if(rx_message.StdId == 0x205)
				{
					
					motor_last[CAN1_M1].ecd_fdb = motor[CAN1_M1].ecd_fdb;
					
					motor[CAN1_M1].ecd_fdb = can_rx_data[0]<<8|can_rx_data[1];
					motor[CAN1_M1].current_fdb = can_rx_data[2]<<8|can_rx_data[3];
					motor[CAN1_M1].current_set_fdb = can_rx_data[4]<<8|can_rx_data[5];
					motor[CAN1_M1].hall_fdb = can_rx_data[6];

					if(motor_flag[CAN1_M1]<=0)
					{
						if(motor_last[CAN1_M1].ecd_fdb-motor[CAN1_M1].ecd_fdb <= -4000)
							motor[CAN1_M1].motor_num_of_turn--;
						if(motor_last[CAN1_M1].ecd_fdb-motor[CAN1_M1].ecd_fdb >= 4000)
							motor[CAN1_M1].motor_num_of_turn++;
					}
					else 
					{
						motor_flag[CAN1_M1]--;
					}					
					__HAL_CAN_CLEAR_FLAG(&hcan1, CAN_FLAG_FOV0);
					
				}else	if(rx_message.StdId == 0x206)
				{
					
					motor_last[CAN1_M2].ecd_fdb = motor[CAN1_M2].ecd_fdb;
					
					motor[CAN1_M2].ecd_fdb = can_rx_data[0]<<8|can_rx_data[1];
					motor[CAN1_M2].current_fdb = can_rx_data[2]<<8|can_rx_data[3];
					motor[CAN1_M2].current_set_fdb = can_rx_data[4]<<8|can_rx_data[5];
					motor[CAN1_M2].hall_fdb = can_rx_data[6];

					if(motor_flag[CAN1_M1]<=0)
					{
						if(motor_last[CAN1_M2].ecd_fdb-motor[CAN1_M2].ecd_fdb <= -4000)
							motor[CAN1_M2].motor_num_of_turn--;
						if(motor_last[CAN1_M2].ecd_fdb-motor[CAN1_M2].ecd_fdb >= 4000)
							motor[CAN1_M2].motor_num_of_turn++;
					}
					else 
					{
						motor_flag[CAN1_M1]--;
					}					
					
//					__HAL_CAN_CLEAR_FLAG(&hcan1, CAN_FLAG_FOV0);
				}
				
			}

			
		}
		
		
		if(hcan ==&hcan2)
    {
			HAL_RetVal = HAL_CAN_GetRxMessage(&hcan2, CAN_RX_FIFO0, &rx_message,can_rx_data);
			
			if(HAL_RetVal == HAL_OK){
				
				if(rx_message.StdId == 0x206)
				{
					
					motor_last[CAN1_M3].ecd_fdb = motor[CAN1_M3].ecd_fdb;
					
					motor[CAN1_M3].ecd_fdb = can_rx_data[0]<<8|can_rx_data[1];
					motor[CAN1_M3].current_fdb = can_rx_data[2]<<8|can_rx_data[3];
					motor[CAN1_M3].current_set_fdb = can_rx_data[4]<<8|can_rx_data[5];
					motor[CAN1_M3].hall_fdb = can_rx_data[6];

					if(motor_flag[CAN1_M3]<=0)
					{
						if(motor_last[CAN1_M3].ecd_fdb-motor[CAN1_M3].ecd_fdb <= -4000)
							motor[CAN1_M3].motor_num_of_turn--;
						if(motor_last[CAN1_M3].ecd_fdb-motor[CAN1_M3].ecd_fdb >= 4000)
							motor[CAN1_M3].motor_num_of_turn++;
					}
					else 
					{
						motor_flag[CAN1_M3]--;
					}					
					__HAL_CAN_CLEAR_FLAG(&hcan1, CAN_FLAG_FOV0);
					
				}
				
			}

		}

}
uint32_t error=0;
uint32_t error2=0;
uint8_t error_flag=0;
void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan) {
		error_flag++;
    
		error = HAL_CAN_GetError(&hcan1);
    // �������������CAN��
    if (error & HAL_CAN_ERROR_BOF) {
        HAL_CAN_Stop(&hcan1);
        HAL_CAN_Start(&hcan1);
    }
		
    error2 = HAL_CAN_GetError(&hcan2);
    // �������������CAN��
    if (error2 & HAL_CAN_ERROR_BOF) {
        HAL_CAN_Stop(&hcan2);
        HAL_CAN_Start(&hcan2);
    }		
	
}















/* USER CODE END 1 */
